package com.spx.core.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

import com.spx.kernel.types.ModelType;

public abstract class DeleteModelTypeHandler<T extends ModelType> extends AbstractHandler
{

	protected ExecutionEvent event;
	protected Shell shell;

	@SuppressWarnings("unchecked")
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException
	{
		final IStructuredSelection selection = getSelection(event);

		for (final Object object : selection.toArray())
		{
			if (object instanceof ModelType)
			{
				this.event = event;
				shell = HandlerUtil.getActiveWorkbenchWindow(event).getShell();
				final T user = (T) object;
				final boolean doDelete = MessageDialog.openQuestion(shell, "Confirm  Deletion", "Really Delete user: " + user.getName());
				if (doDelete)
				{
					delete(user);
				}
			}

		}

		return null;
	}

	protected abstract void delete(T model);

	private IStructuredSelection getSelection(ExecutionEvent event)
	{
		final IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
		return selection;
	}
}
